﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
#if UNITY_EDITOR
using UnityEditor;
#endif


[Serializable]
public class GT_NodeGraph : ScriptableObject
{
    #region Variables
    public string graphName = "New Graph";
    public List<GT_NodeBase> nodes;
    public GT_NodeBase selectedNode;

    public bool wantsConnection = false;
    public GT_NodeBase connectionNode;
    public bool showProperties = false;
    #endregion

    #region Main Methods
    private void OnEnable()
    {
        if(nodes == null)
        {
            nodes = new List<GT_NodeBase>();
        }
    }

    public void InitGraph()
    {
        if(nodes.Count > 0)
        {
            for(int i = 0; i > nodes.Count; i++)
            {
                nodes[i].InitNode();
            }
        }
    }

    public void UpdateGraph()
    {
        
    }

    #if UNITY_EDITOR
    public void UpdateGraphGUI(Event e, Rect viewRect, GUISkin viewSkin)
    {
        if(nodes.Count > 0)
        {
            ProcessingEvents(e, viewRect);
            for(int i = 0; i < nodes.Count; i++)
            {
                nodes[i].UpdateNodeGUI(e, viewRect, viewSkin);
            }
        }

        if (wantsConnection)
        {
            if(connectionNode != null)
            {
                DrawConnectionToMouse(e.mousePosition);
            }
        }

        if (e.type == EventType.Layout)
        {
            if(selectedNode != null)
            {
                showProperties = true;
            }
            
        }

        EditorUtility.SetDirty(this);

    }
    #endif

    #endregion

    #region Unitility Methods
    void ProcessingEvents(Event e, Rect viewRect)
    {
        if (viewRect.Contains(e.mousePosition))
        {
            if(e.button == 0)
            {
                if (e.type == EventType.MouseDown)
                {
                    DeselectAllNodes();
                    bool setNode = false;
                    selectedNode = null;
                    showProperties = false;
                    foreach (GT_NodeBase node in nodes)
                    {
                        if (node.nodeRect.Contains(e.mousePosition))
                        {
                            node.isSelected = true;
                            selectedNode = node;
                            setNode = true;
                        }
                    }
                    if (!setNode)
                    {
                        //DeselectAllNodes();
                        //wantsConnection = false;
                        //connectionNode = null;
                    }
                    if (wantsConnection)
                    {
                        wantsConnection = false;
                    }
                }
            }
        }
    }

    void DeselectAllNodes()
    {
        foreach(GT_NodeBase node in nodes)
        {
            node.isSelected = false;
        }
    }

    private void DrawConnectionToMouse(Vector2 mousePosition)
    {
        Handles.BeginGUI();

        Handles.color = Color.white;
        Handles.DrawLine(new Vector3(connectionNode.nodeRect.x + connectionNode.nodeRect.width + 24f,
                                    connectionNode.nodeRect.y + connectionNode.nodeRect.height * 0.5f, 0f),
                        new Vector3(mousePosition.x, mousePosition.y, 0f));

        Handles.BeginGUI();
    }

    #endregion
}
